<template>
  <div>
    <el-card class="box-card">
      <el-alert
        title="注意：只允许为第三级分类设置相关参数！"
        type="warning"
        show-icon
      >
      </el-alert>
      <!-- 级联选择器 -->
      <el-form>
        <el-form-item label="选择商品分类">
          <el-cascader
            size="mini"
            v-model="value"
            :options="options"
            :props="props"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
      </el-form>

      <!-- Tabs 标签页 -->
      <el-tabs v-model="activeName">
        <el-tab-pane label="动态参数" name="first">
          <el-button
            size="small"
            type="primary"
            @click="dialogVisible = true"
            :disabled="status"
            plain
            >添加参数</el-button
          >
          <!-- 表格 -->
          <el-table border :data="tableData" style="width: 100%">
            <el-table-column type="expand">
              <template slot-scope="props">
                <el-tag
                  :key="index"
                  v-for="(item, index) in props.row.attr_vals"
                  closable
                  :disable-transitions="false"
                  @close="handleClose(props.row, item)"
                >
                  <!-- {{ props.row.attr_vals }} -->
                  {{ item }}
                </el-tag>
                <el-input
                  class="input-new-tag"
                  v-if="props.row.inputVisible"
                  v-model="inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="handleInputConfirm(props.row)"
                  @blur="handleInputConfirm(props.row)"
                >
                </el-input>
                <el-button
                  v-else
                  class="button-new-tag"
                  size="small"
                  @click="showInput(props.row)"
                  >+ New Tag</el-button
                >
              </template>
            </el-table-column>
            <el-table-column label="#" type="index" width="50">
            </el-table-column>
            <el-table-column label="参数名称" prop="attr_name">
            </el-table-column>
            <el-table-column label="操作" width="300">
              <template slot-scope="scope">
                <el-button type="primary" @click="editClass(scope.row)" round
                  >编辑</el-button
                >
                <el-button type="danger" @click="deleteClass(scope.row)" round
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>

        <el-tab-pane label="静态属性" name="second">
          <el-button
            size="small"
            type="primary"
            :disabled="status"
            @click="dialogVisible3 = true"
            plain
            >添加属性</el-button
          >
          <!-- 表格 -->
          <el-table border :data="tableData2" style="width: 100%">
            <el-table-column type="expand">
              <template slot-scope="props">
                <el-tag
                  :key="index"
                  v-for="(item, index) in props.row.attr_vals"
                  closable
                  :disable-transitions="false"
                  @close="handleClose2(props.row, item)"
                >
                  <!-- {{ props.row.attr_vals }} -->
                  {{ item }}
                </el-tag>
                <el-input
                  class="input-new-tag"
                  v-if="props.row.inputVisible"
                  v-model="inputValue2"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="handleInputConfirm2(props.row)"
                  @blur="handleInputConfirm2(props.row)"
                >
                </el-input>
                <el-button
                  v-else
                  class="button-new-tag"
                  size="small"
                  @click="showInput2(props.row)"
                  >+ New Tag</el-button
                >
              </template>
            </el-table-column>
            <el-table-column label="#" type="index" width="50">
            </el-table-column>
            <el-table-column label="属性名称" prop="attr_name">
            </el-table-column>
            <el-table-column label="操作" width="300">
              <template slot-scope="scope">
                <el-button type="primary" @click="editClass2(scope.row)" round
                  >编辑</el-button
                >
                <el-button type="danger" @click="deleteClass2(scope.row)" round
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>

      <!-- 添加静态参数弹出框 -->
      <el-dialog title="添加动态参数" :visible.sync="dialogVisible" width="50%">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="动态参数" prop="attr_name">
            <el-input v-model="ruleForm.attr_name"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="attrOk">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 静态参数编辑弹出框 -->
      <el-dialog
        title="修改动态参数"
        :visible.sync="dialogVisible2"
        width="50%"
      >
        <el-form
          :model="ruleForm2"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="动态参数" prop="attr_name">
            <el-input v-model="ruleForm2.attr_name"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible2 = false">取 消</el-button>
          <el-button type="primary" @click="ModClass">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 添加静态属性弹出框 -->
      <el-dialog
        title="添加静态属性"
        :visible.sync="dialogVisible3"
        width="50%"
      >
        <el-form
          :model="ruleForm3"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="静态属性" prop="attr_name">
            <el-input v-model="ruleForm3.attr_name"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible3 = false">取 消</el-button>
          <el-button type="primary" @click="AddProp">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 编辑静态属性弹出框 -->
      <el-dialog
        title="修改静态属性"
        :visible.sync="dialogVisible4"
        width="50%"
      >
        <el-form
          :model="ruleForm4"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="静态属性" prop="attr_name">
            <el-input v-model="ruleForm4.attr_name"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible4 = false">取 消</el-button>
          <el-button type="primary" @click="ModClass2">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import {
  getClass2,
  parameter,
  AddParameter,
  AddParameter2,
  ModClass,
  deleteClass,
  DelParameter,
} from "@/utils/api";
export default {
  name: "APP",
  data() {
    return {
      value: [],
      options: [],
      props: {
        expandTrigger: "hover",
        value: "cat_id",
        label: "cat_name",
        children: "children",
      },
      activeName: "first",
      inputValue: "",
      tableData: [], //动态参数数据
      status: false, //添加按钮默认状态
      select: [], //选择的数组
      dialogVisible: false, //添加弹出框默认状态
      dialogVisible2: false, //编辑弹出框默认状态
      dialogVisible3: false, //静态属性弹出框默认状态
      dialogVisible4: false, //静态属性编辑弹出框默认状态
      ruleForm: {
        attr_name: "", //参数名称
      },
      ruleForm2: {
        attr_name: "",
      },
      ruleForm3: {
        attr_name: "",
      },
      ruleForm4: {
        attr_name: "",
      },
      rules: {
        attr_name: [
          { required: true, message: "请输入参数名称", trigger: "blur" },
        ],
      },

      tableData2: [], //静态属性数据
      inputValue2: "", //
    };
  },
  methods: {
    //封装动态参数请求
    GetClass() {
      let params = {
        id: this.value[2],
        sel: "many",
      };
      parameter(params).then((res) => {
        // console.log(res.data);
        if (res.meta.status != 200) {
          this.$message.error("动态参数获取失败");
        } else {
          res.data.forEach((item) => {
            item.attr_vals = item.attr_vals ? item.attr_vals.split(",") : [];
            item.inputVisible = false
          });
          this.tableData = res.data;
        }
      });
    },

    //封装静态属性请求
    GetClass2() {
      let params = {
        id: this.value[2],
        sel: "only",
      };
      parameter(params).then((res) => {
        if (res.meta.status != 200) {
          this.$message.error("动态参数获取失败");
        } else {
          res.data.forEach((item) => {
            item.attr_vals = item.attr_vals ? item.attr_vals.split(",") : [];
            item.inputVisible = false
          });
          this.tableData2 = res.data;
        }
      });
    },

    //级联选择器切换事件
    handleChange(value) {
      this.GetClass();
      this.GetClass2();
    },

    //添加商品分类/*/*/*/*/*/
    handleInputConfirm(row) {
      // console.log(row);
      row.inputVisible = false
      if (this.inputValue == "") return false;
      row.attr_vals.push(this.inputValue);
      let params = {
        id: row.attr_id,
        attr_name: row.attr_name,
        attr_sel: "many",
        attr_vals: row.attr_vals.join(","),
      };

      AddParameter(params).then((res) => {
        // console.log(res)
        if (res.meta.status == 201) {
          this.$message({
            message: "动态参数创建成功！",
            type: "success",
          });
        }
      });

      this.inputValue = "";
    },

    //移除动态参数事件
    handleClose(val, name) {
      // console.log(val);
      // console.log(name);
      this.tableData.forEach((item) => {
        item.attr_vals = item.attr_vals.filter((obj) => obj != name);
      });
      DelParameter(val).then((res) => {
        console.log(res);
        if (res.meta.status == 200) {
          this.$message({
            message: "动态参数删除成功！",
            type: "success",
          });
        } else {
          this.$message.error("动态参数删除失败!");
        }
      });
    },
    //移除静态属性事件
    handleClose2(val, name) {
      this.tableData2.forEach((item) => {
        item.attr_vals = item.attr_vals.filter((obj) => obj != name);
      });
      DelParameter(val).then((res) => {
        console.log(res);
        if (res.meta.status == 200) {
          this.$message({
            message: "动态参数删除成功！",
            type: "success",
          });
        } else {
          this.$message.error("动态参数删除失败!");
        }
      });
    },

    //点击变input事件
    showInput(val) {
      val.inputVisible = true
      this.select = val;
      this.$nextTick((_) => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    //添加参数
    attrOk() {
      // console.log(this.value);
      let params = {
        id: this.value[2],
        attr_name: this.ruleForm.attr_name,
        attr_sel: "many",
      };
      AddParameter2(params).then((res) => {
        console.log(res);
        this.dialogVisible = false;
        if (res.meta.status == 201) {
          this.GetClass();
          this.$message({
            message: "添加动态参数成功",
            type: "success",
          });
        } else {
          this.$message.error("添加动态参数失败");
        }
      });
    },

    //编辑动态参数
    editClass(val) {
      // console.log(val);
      this.dialogVisible2 = true;
      this.ruleForm2 = val;
    },

    //确定修改动态参数事件
    ModClass() {
      ModClass(this.ruleForm2).then((res) => {
        // console.log(res);
        this.dialogVisible2 = false;
        if (res.meta.status == 200) {
          this.GetClass2();
          this.$message({
            message: "参数更新成功",
            type: "success",
          });
        } else {
          this.$message.error("参数更新失败");
        }
      });
    },

    //删除动态参数
    deleteClass(val) {
      //  console.log(val);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteClass(val).then((res) => {
            this.GetClass();
            if (res.meta.status == 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            } else {
              this.$message.error("删除失败！");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //添加静态属性
    handleInputConfirm2(row) {
      row.inputVisible = false
      if (this.inputValue2 == "") return false;
      row.attr_vals.push(this.inputValue2);
      let params = {
        id: row.attr_id,
        attr_name: row.attr_name,
        attr_sel: "only",
        attr_vals: row.attr_vals.join(","),
      };

      AddParameter(params).then((res) => {
        // console.log(res);
        if (res.meta.status == 201) {
          this.$message({
            message: "静态属性创建成功！",
            type: "success",
          });
        }
      });

      this.inputValue2 = "";
    },

    //点击变input事件
    showInput2(val) {
      val.inputVisible = true
      this.select = val;
      this.$nextTick((_) => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    //添加静态属性
    AddProp() {
      let params = {
        id: this.value,
        attr_name: this.ruleForm3.attr_name,
        attr_sel: "only",
      };
      AddParameter2(params).then((res) => {
        // console.log(res);
        this.dialogVisible3 = false;
        if (res.meta.status == 201) {
          this.GetClass2();
          this.$message({
            message: "添加动态参数成功",
            type: "success",
          });
        } else {
          this.$message.error("添加动态参数失败");
        }
      });
    },

    //编辑静态属性数据回填
    editClass2(val) {
      this.dialogVisible4 = true;
      this.ruleForm4 = val;
    },

    //静态属性更新
    ModClass2() {
      ModClass(this.ruleForm4).then((res) => {
        console.log(res);
        this.dialogVisible4 = false;
        if (res.meta.status == 200) {
          this.GetClass();
          this.$message({
            message: "参数更新成功",
            type: "success",
          });
        } else {
          this.$message.error("参数更新失败");
        }
      });
    },

    //删除静态属性
    deleteClass2(val) {
      // console.log(val);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteClass(val).then((res) => {
            this.GetClass2();
            if (res.meta.status == 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            } else {
              this.$message.error("删除失败！");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  created() {
    //商品分类请求
    getClass2().then((res) => {
      this.options = res.data;
    });
  },
  mounted() {},
  //监听属性
  watch: {
    value: {
      handler(val1, val2) {
        if (val1.length == 0) {
          this.status = true;
        } else {
          this.status = false;
        }
      },
      immediate: true,
    },
  },
};
</script>
<style lang="scss" scoped>
.el-alert {
  margin-bottom: 10px;
}

.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
